<template>
    <div class="form-block-inner">
        <Divider orientation="left">设定评价</Divider>
        <Form :model="formItem" label-position="top" ref="formItem">
            <div v-for="(val,index) in dataList" :key="index">
                <div class="title-style">流程节点{{index+1}}-{{ val.node_title }}</div>
                <div v-for="(item,index2) in val.evaluateList" :key="index2">
                    <Row :gutter="16">
                        <Col span="10">
                            <FormItem class="required" prop="pj_zhu_ti" label="评价主体:">
                                <div class="form-element">
                                    <Input v-model="item.pj_zhu_ti" placeholder="请输入评价主体"/>
                                </div>
                            </FormItem>
                        </Col>
                        <Col span="10">
                            <FormItem prop="pj_fan_wei" label="评价内容:">
                                <div class="form-element">
                                    <Input v-model="item.pj_fan_wei" placeholder="服务评价、效能评价"/>
                                </div>
                            </FormItem>
                        </Col>
                        <Col span="4">
                            <FormItem label=" " class="col-last-btn">
                                <Button @click="delAction(val, index2)" type="error" shape="circle"
                                        icon="md-close"></Button>
                            </FormItem>
                        </Col>
                    </Row>
                    <Row :gutter="16">
                        <Col span="10">
                            <Row :gutter="16">
                                <Col span="24">
                                    <FormItem prop="pj_fan_kui" label="反馈方式:">
                                        <div class="form-element">
                                            <Input v-model="item.pj_fan_kui" placeholder="微信、电话、系统消息"/>
                                        </div>
                                    </FormItem>
                                </Col>
                                <Col span="24">
                                    <FormItem prop="pj_gui_ze" label="评价规则:">
                                        <div>
                                            <Input v-model="item.pj_gui_ze" type="textarea" :autosize="{minRows: 5,maxRows: 5}"
                                                   placeholder="办事人可以评价、所有人可以评价"/>
                                        </div>
                                    </FormItem>
                                </Col>
                            </Row>
                        </Col>
                        <Col span="10">
                            <Row :gutter="16">
                                <Col span="24">
                                    <FormItem prop="pj_fang_shi" label="评价方式:">
                                        <div class="form-element">
                                            <!--<Input v-model="item.pj_fang_shi" placeholder="在线客服、满意问卷、留言评价"/>-->
                                            <Select clearable v-model="formItem.pj_fang_shi" style="width:100%">
                                                <Option value="系统留言">系统留言</Option>
                                                <Option value="系统问卷">系统问卷</Option>
                                            </Select>
                                        </div>
                                    </FormItem>
                                </Col>
                                <Col span="24" v-if="formItem.pj_fang_shi == '系统问卷'">
                                    <FormItem prop="pj_content" label="设定调查问卷内容:">
                                        <div>
                                            <Input v-model="item.pj_content" type="textarea" :autosize="{minRows: 5,maxRows: 5}"
                                                   placeholder="请输入内容"/>
                                        </div>
                                    </FormItem>
                                </Col>
                            </Row>
                        </Col>
                    </Row>
                </div>
                <Row>
                    <Col span="24">
                        <Button type="success" @click="add(val.evaluateList)">添加</Button>
                    </Col>
                </Row>
            </div>
            <br>
            <Row>
                <Col span="24">
                    <div class="btn-style" v-if="dataList.length >0">
                        <common-save-btn :base-info-id="baseInfoId" :form-data="dataList" dbName="GovEvaluate"
                                         param-filter="evaluateList"></common-save-btn>
                    </div>
                </Col>
            </Row>
        </Form>
        <br>
        <Form :model="pubData" :rules="ruleValidate" label-position="top" ref="pubData">
            <Divider orientation="left">设定公示</Divider>
            <Row :gutter="16">
                <Col span="10">
                    <FormItem prop="publicity_content" label="公示内容:">
                        <div>
                            <Input v-model="pubData.publicity_content" type="textarea"
                                   :autosize="{minRows: 5,maxRows: 5}"
                                   placeholder="请输入内容"/>
                        </div>
                    </FormItem>
                </Col>
                <Col span="10">
                    <FormItem prop="publicity_media" label="公示媒介:">
                        <div>
                            <Input v-model="pubData.publicity_media" type="textarea" :autosize="{minRows: 5,maxRows: 5}"
                                   placeholder="请输入内容"/>
                        </div>
                    </FormItem>
                </Col>
                <Col span="10">
                    <FormItem prop="publicity_scope" label="公示范围:">
                        <div>
                            <Input v-model="pubData.publicity_scope" type="textarea" :autosize="{minRows: 5,maxRows: 5}"
                                   placeholder="请输入内容"/>
                        </div>
                    </FormItem>
                </Col>
                <Col span="10">
                    <FormItem prop="publicity_cycle" label="公示周期:">
                        <div>
                            <Input v-model="pubData.publicity_cycle" type="textarea" :autosize="{minRows: 5,maxRows: 5}"
                                   placeholder="请输入内容"/>
                        </div>
                    </FormItem>
                </Col>
            </Row>
            <br>
            <Row>
                <Col span="24">
                    <div class="btn-style">
                        <Button type="primary" @click="saveBtnAction" :loading="btnLoading">保存</Button>
                    </div>
                </Col>
            </Row>
        </Form>
    </div>
</template>
<script>
import {
  updateSstGovBase,
  selectSstGovBaseById
} from '@/api/setting/base-info'
import { select } from '@/api/setting'
import CommonSaveBtn from '@/components/common-save-btn'

export default {
  name: 'EvaluateSetting',
  components: { CommonSaveBtn },
  props: {
    options: {
      type: Array,
      default: () => {
        return []
      }
    },
    tabName: {
      type: String,
      default: () => {
        return ''
      }
    },
    baseInfoId: {
      type: String,
      default: () => {
        return ''
      }
    }
  },
  data () {
    return {
      ruleValidate: {
        // publicity_content: [
        //   { required: true, message: '公示内容不能为空', trigger: 'blur' }
        // ],
        // publicity_media: [
        //   { required: true, message: '公示媒介不能为空', trigger: 'blur' }
        // ],
        // publicity_scope: [
        //   { required: true, message: '公示范围不能为空', trigger: 'blur' }
        // ],
        // publicity_cycle: [
        //   { required: true, message: '公示周期不能为空', trigger: 'blur' }
        // ]
      },
      formItem: {},
      pubData: {},
      dataList: [],
      btnLoading: false
    }
  },
  methods: {
    init () {
      if (this.tabName === '17') {
        this.bottomInfo()
        let param = { base_info_id: this.baseInfoId }
        select('GovEvaluate', param).then(res => {
          this.loading = false
          if (res.data.errcode === 0) {
            for (let i = 0; i < this.dataList.length; i++) {
              this.dataList[i].evaluateList = []
              for (let j = 0; j < res.data.data.length; j++) {
                if (this.dataList[i].id === res.data.data[j].gov_flow_id) {
                  res.data.data[j].base_info_id = this.baseInfoId
                  this.dataList[i].evaluateList.push(res.data.data[j])
                }
              }
            }
            this.dataList = this.$common.xeUtils.clone(this.dataList)
          } else {
            this.$Message.error('数据获取失败!')
          }
        }).catch(ret => {
          this.loading = false
          // this.$Message.error(ret)
        })
      }
    },
    bottomInfo () {
      // 底部信息获取信息
      selectSstGovBaseById({ id: this.baseInfoId }).then(ret => {
        let data = ret.data
        if (data.errcode === 0) {
          this.pubData = data.data
        } else {
          this.error(data)
        }
      })
    },
    success (val) {
      val = this.$common.xeUtils.isEmpty(val) ? '操作成功' : val
      this.$Message.success(val)
    },
    error (data) {
      this.$Message.error({
        content: '操作失败,\n' + data.errmsg
      })
    },
    add (val) {
      val.push({})
      this.dataList = this.$common.xeUtils.clone(this.dataList)
    },
    delAction (item, index) {
      item.evaluateList.splice(index, 1)
      this.dataList = this.$common.xeUtils.clone(this.dataList)
    },
    // 保存公示信息
    saveBtnAction () {
      if (!this.$route.query.id) {
        this.$Message.error('缺少参数')
        return false
      }
      let param = {
        ...this.pubData,
        id: this.$route.query.id
      }
      // 数据校验
      this.$refs['pubData'].validate((valid) => {
        if (valid) {
          this.updateDataAction(param)
        } else {
          this.$Message.error({
            content: '操作失败,请核对添加信息后重试'
          })
        }
      })
    },
    updateDataAction (param) {
      this.btnLoading = true
      updateSstGovBase(param).then(ret => {
        let data = ret.data
        if (data.errcode === 0) {
          this.btnLoading = false
          this.success()
        } else {
          this.btnLoading = false
          this.error(data)
        }
      })
    }
  },
  watch: {
    options (data) {
      this.dataList = data
    },
    dataList (data) {
      this.$emit('result', data)
    },
    tabName (data) {
      this.init()
    },
    baseInfoId (data) {
      this.baseInfoId = data
    }
  }
}
</script>

<style scoped>
    .col-last-btn {
        float: right;
        margin-right: 40px;
    }

    .required /deep/ .ivu-form-item-label:before {
        content: '*';
        display: inline-block;
        margin-right: 4px;
        line-height: 1;
        font-family: SimSun;
        font-size: 14px;
        color: #ed4014;
    }
</style>
